/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-07-09 19:15:34
 * @LastEditTime: 2022-07-14 23:17:06
 * @LastEditors: liutq
 * @Reference:
 */
package banner

import (
	"time"

	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/model"
)

type Banner struct {
	ID         uint      `gorm:"column:id; parimary_key" json:"id"`
	BannerUrl  string    `gorm:"column:banner_url" json:"banner_url"`
	Title      string    `gorm:"column:title" json:"title"`
	JumpUrl    string    `gorm:"column:jump_url" json:"jump_url"`
	CreateTime time.Time `gorm:"column:create_time" json:"create_time" `
	Status     uint8     `gorm:"column:status" json:"status"`
}

type BannerList struct {
	List []Banner
}

func GetBannerList() *BannerList {
	var list BannerList

	model.Db.Model(&Banner{}).Where(&Banner{Status: 1}).Scan(&list.List)
	return &list

}

func AddOrModifyBanner(param common.Banner) error {
	if param.ID > 0 {
		updateinfo := make(map[string]interface{})

		updateinfo["banner_url"] = param.BannerUrl
		updateinfo["title"] = param.Title
		updateinfo["jump_url"] = param.JumpUrl
		updateinfo["status"] = param.Status
		return model.Db.Model(&Banner{}).Where(&Banner{ID: param.ID}).Update(updateinfo).Error
	}
	param.CreateTime = time.Now().UTC()
	return model.Db.Model(&Banner{}).Create(&param).Error

}

func GetBannerListForAdmin() *BannerList {
	var list BannerList

	model.Db.Model(&Banner{}).Scan(&list.List)
	return &list

}
